﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;
using Scriptor.Presentation.Enumerator;

namespace Scriptor.Presentation.Templates
{
    public class MethodAssignFilesTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gScope { get; set; }
        public StringBuilder ScopoFormatado { get; set; }

        List<string> gMethod { get; set; }
        public StringBuilder methodFormatado { get; set; }

        public string strUsing(Enums.OrigemEnum source)
        {
            gUsing = new List<string>();

            switch (source)
            {
                case Enums.OrigemEnum.DataContract:
                    break;
                case Enums.OrigemEnum.BusinessEntity:
                    break;
                case Enums.OrigemEnum.BusinessLogic:
                    {
                        gUsing.Add("using System;");
                        gUsing.Add("using System.Collections.Generic;");
                        gUsing.Add("using System.Text;");
                        gUsing.Add("using System.ComponentModel;");
                        gUsing.Add("using Sinacor.Infra.Service.Validation;");
                        gUsing.Add("using Sinacor.Infra.Service.Common;");
                        gUsing.Add("using Sinacor.Infra.Service.ExceptionHandling;");
                    }
                    break;
                case Enums.OrigemEnum.ResourceAccess:
                    {
                        gUsing.Add("using System;");
                        gUsing.Add("using System.Collections.Generic;");
                        gUsing.Add("using System.Text;");
                        gUsing.Add("using Sinacor.Infra.Service.Data;");
                        gUsing.Add("using System.Data.Common;");
                        gUsing.Add("using System.Data;");
                        gUsing.Add("using Sinacor.Infra.Service.Security.Authorization;");
                    }
                    break;
                default:
                    break;
            }

            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strScope(string pNameSpace, string pClassName, Enums.OrigemEnum source, bool showInheriting)
        {
            gScope = new List<string>();

            gScope.Add("namespace " + pNameSpace);
            gScope.Add("{");
            string temp = "\tpublic class " + pClassName;

            if (showInheriting)
            {
                switch (source)
                {
                    case Enums.OrigemEnum.DataContract:
                        break;
                    case Enums.OrigemEnum.BusinessEntity:
                        break;
                    case Enums.OrigemEnum.BusinessLogic:
                        { temp = temp + " : " + "BaseBusinessLogic"; }
                        break;
                    case Enums.OrigemEnum.ResourceAccess:
                        { temp = temp + " : " + "BaseResourceAccess"; }
                        break;
                    default:
                        break;
                }
            }

            gScope.Add(temp);

            gScope.Add("\t{");
            gScope.Add("@@class-body");
            gScope.Add("\t}");
            gScope.Add("}");

            ScopoFormatado = new StringBuilder();

            foreach (string str in gScope)
            {
                ScopoFormatado.Append(str);
                ScopoFormatado.Append(Environment.NewLine);
            }

            return ScopoFormatado.ToString();
        }

        public string strMethod(Method mtd)
        {
            gMethod = new List<string>();

            string scope = "public ";

            string retorno = string.Empty;

            if (mtd.ReturnIsList)
            {
                retorno = "List<BusinessEntity." + mtd.ReturnName + "> ";
            }
            else
            {
                retorno = "BusinessEntity." + mtd.ReturnName + " ";
            }

            if (string.IsNullOrEmpty(mtd.ReturnName))
                retorno = " void ";

            string parameter = string.Empty;

            if(mtd.ParamList.Count == 0)
                parameter = " () ";
            else
                if (mtd.ParamList.Count == 1)
                {
                    if (mtd.ParamList[0].ParameterIsList)
                    {
                        if (!mtd.ParamList[0].ParameterIsParameter)
                            parameter = "(List<BusinessEntity." + mtd.ParamList[0].ParameterType + "> " + mtd.ParamList[0].ParameterName.ToLower() + ")";
                        else
                            parameter = "(List<BusinessEntity.Parameter." + mtd.ParamList[0].ParameterType + "> " + mtd.ParamList[0].ParameterName.ToLower() + ")";
                    }
                    else
                    {
                        if (!mtd.ParamList[0].ParameterIsParameter)
                            parameter = "(BusinessEntity." + mtd.ParamList[0].ParameterType + " " + mtd.ParamList[0].ParameterName.ToLower() + ")";
                        else
                            parameter = "(BusinessEntity.Parameter." + mtd.ParamList[0].ParameterType + " " + mtd.ParamList[0].ParameterName.ToLower() + ")";
                    }
                }
                else
                {
                    parameter = "( ";

                    foreach (Param param in mtd.ParamList)
                    {
                        if (param.ParameterIsList)
                        {
                            if (!param.ParameterIsParameter)
                                parameter += "List<BusinessEntity." + param.ParameterType + "> " + param.ParameterName.ToLower();
                            else
                                parameter += "List<BusinessEntity.Parameter." + param.ParameterType + "> " + param.ParameterName.ToLower();
                        }
                        else
                        {
                            if (!param.ParameterIsParameter)
                                parameter += "BusinessEntity." + param.ParameterType + " " + param.ParameterName.ToLower();
                            else
                                parameter += "BusinessEntity.Parameter." + param.ParameterType + " " + param.ParameterName.ToLower();
                        }

                        parameter += " , ";
                    }

                    parameter = parameter.Substring(0, parameter.Length - 2);
                    parameter += ")";
                }

            gMethod.Add(scope + retorno + mtd.MethodName + " " + parameter);

            methodFormatado = new StringBuilder();

            foreach (string str in gMethod)
            {
                //Adicionado: 21/8
                if (mtd.MethodComent.Trim().Length > 0)
                {
                    methodFormatado.Append("\t\t/// <summary>" + Environment.NewLine);
                    methodFormatado.Append("\t\t///" + mtd.MethodComent.Replace("\n", "\n\t\t///") + Environment.NewLine);
                    methodFormatado.Append("\t\t/// </summary>" + Environment.NewLine);
                }
                //Fim Adicionado. phsilva

                methodFormatado.Append("\t\t" + str);
                methodFormatado.Append(Environment.NewLine);
                methodFormatado.Append("\t\t{");
                methodFormatado.Append(Environment.NewLine);
                methodFormatado.Append("\t\t}");
                methodFormatado.Append(Environment.NewLine);

            }

            return methodFormatado.ToString();
        }

        public string AppendMethod(string pScope, StringBuilder stb)
        {
            return pScope.ToString().Replace("@@class-body", stb.ToString());
        }
    }
}
